package com.springboot.mail.modules.contact.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.springboot.mail.common.ResultUtil;
import com.springboot.mail.common.bean.DeleteDTO;
import com.springboot.mail.common.bean.MyResult;
import com.springboot.mail.common.bean.PageQueryDTO;
import com.springboot.mail.modules.contact.domain.entity.ContactDO;
import com.springboot.mail.modules.contact.domain.vo.ContactVO;
import com.springboot.mail.modules.contact.mapper.ContactMapper;
import com.springboot.mail.modules.contact.service.ContactService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ContactServiceImpl extends MPJBaseServiceImpl<ContactMapper, ContactDO> implements ContactService {

    @Resource
    private ContactMapper contactMapper;

    @Override
    public MyResult<Page<ContactVO>> getInfoPageBy(PageQueryDTO pageQueryDTO, ContactVO contactVO) {

        String userId = this.getLoginUserId();
        System.out.println("userId = " + userId);

        // 连表求总数。s
        MPJLambdaWrapper<ContactDO> countWrapper = new MPJLambdaWrapper<ContactDO>().eq(ContactDO::getUserId, userId);
        Long total = contactMapper.selectJoinCount(countWrapper);

        // 连表分页查询。
        MPJLambdaWrapper<ContactDO> lambdaWrapper = new MPJLambdaWrapper<ContactDO>().selectAll(ContactDO.class).eq(ContactDO::getUserId, userId);

        Page<ContactVO> userVOIPage = contactMapper.selectJoinPage(
                pageQueryDTO.toPage(pageQueryDTO.getCurrent(), pageQueryDTO.getSize(), total, false)
                , ContactVO.class, lambdaWrapper);
        System.out.println(userVOIPage.getRecords());//获取分页记录
        System.out.println(userVOIPage.getPages());//总页数
        System.out.println(userVOIPage.getTotal());//总记录数
        System.out.println(userVOIPage.hasPrevious());//是否有上一页
        System.out.println(userVOIPage.hasNext());//是否有下一页
        return ResultUtil.success(userVOIPage);
    }

    @Override
    public MyResult<ContactVO> getInfoByContactId(String contactId) {

        return null;
    }

    @Override
    public MyResult<String> addInfo(ContactVO contactVO) {
        ContactDO contactDO = contactMapper.selectOne(new LambdaQueryWrapper<ContactDO>().eq(ContactDO::getEmail, contactVO.getEmail()));
        if (contactDO != null) {
            return ResultUtil.error("通讯人已存在！请勿重复添加！");
        }

        // 默认值。
        contactVO.setContactId(null);
        contactVO.setUpdateDate(new Date());
        contactVO.setUserId(this.getLoginUserId());
        return contactMapper.insert(contactVO) > 0 ? ResultUtil.success("通讯人添加成功！") : ResultUtil.error("通讯人添加失败！");
    }

    @Override
    public MyResult<String> updateInfo(ContactVO contactVO) {
//        ContactDO contactDO = contactMapper.selectOne(new LambdaQueryWrapper<ContactDO>().eq(ContactDO::getEmail, contactVO.getEmail()));
//        if (contactDO == null || StringUtils.isEmpty(contactVO.getContactId())) {
//            return ResultUtil.error("数据错误啦！该通讯人不存在！");
//        }

        // 默认值。
        contactVO.setUpdateDate(new Date());
        contactVO.setUserId(this.getLoginUserId());
        return contactMapper.updateById(contactVO) > 0 ? ResultUtil.success("通讯人修改成功！") : ResultUtil.error("通讯人修改失败！");
    }

    @Override
    public MyResult<String> deleteInfo(String contactId) {
        return contactMapper.deleteById(contactId) > 0 ? ResultUtil.success("删除通讯人成功！") : ResultUtil.error("删除通讯人失败！");
    }

    @Override
    public MyResult<String> deleteInfoList(DeleteDTO<String> contactIds) {
        return contactMapper.deleteByIds(contactIds.getIdList()) > 0 ? ResultUtil.success("删除通讯人成功！") : ResultUtil.error("删除通讯人失败！");
    }

    /**
     * 获取当前登录的用户ID
     *
     * @return
     */
    private String getLoginUserId() {
        return StpUtil.getLoginId().toString().split("_")[1];
    }
}